#!/bin/bash
# create, edit and view day log
# usage: daylog OPTIONS
# example: daylog // create, edit and view the log of today
#          daylog yesterday // create, edit and view the log of yesterday
#          daylog tomorrow // create, edit and view the log of tomorrow
#          daylog 11/12 // create, edit and view the log of 11/12/current year
#          daylog 11/12/2008 // create, edit and view the log of 11/12/2008 
#
# Copyright (c) 2011 Shubao Liu (shubao.liu@gmail.com)
# All rights reserved.

if [ ! -f ~/.calendar ]; then
  echo -ne "calendar_path="
  read calendar_path
  echo -ne "version_control=(none, svn, git, cvs)"
  read version_control
  echo "calendar_path=$calendar_path" >> ~/.calendar
  echo "version_control=$version_control" >> ~/.calendar
else
  while read line; do
    case "$line" in
      "calendar_path="*) calendar_path=${line##calendar_path=};;
      "version_control="*) version_control=${line##version_control=};;
    esac
  done < ~/.calendar
fi

cd $calendar_path
if [ "$version_control" = "svn" ]; then
  svn up
fi

if [ $# -eq 0 ]; then
  datename=`date +%F`
else
  if [ "$1" = "update" ]; then
    if [ "$version_control" = "svn" ]; then
      svn update
    fi
    exit
  else
    if [ "$1" = "up" ]; then
      if [ "$version_control" = "svn" ]; then
        svn up
      fi
      exit
    else
      datename=`date --date="$1" +%F`
    fi
  fi
fi

logfilename=$datename
if [ -f $logfilename ]; then
  vim $logfilename
  if [ "$version_control" = "svn" ]; then
    svn commit -m "update $logfilename"
  fi
else
  weekday=`date --date="$datename" +%A`
  echo $datename $weekday >> $logfilename
  vim $logfilename
  if [ "$version_control" = "svn" ]; then
    svn add $logfilename
    svn commit -m "add $logfilename"
  fi
fi
